<?php
/**
 * Created by PhpStorm.
 * User: FDG367
 * Date: 2017/9/20
 * Time: 14:30
 */
namespace common\helps;

use common\helps\soap\SoapClient;
use common\models\CustMaster;
use yii;

class Soap extends yii\base\Model{

	public  $mobile;
	public  $pwd;
	public  $wx_openid;
	public  $wx_unionid;
	public  $wx_nickname;
	public  $wx_sex;
	public  $x_province;
	public  $wx_city;
	public  $wx_country;
	public  $wx_headimgurl;
	public  $wx_privilege;
	public  $memo;
	public  $use_flg;
	public  $email;
	public  $creator;
	public  $create_time;
	public  $editor;
	public  $edit_time;
	public  $username;
	public  $member_no;
	public  $member_source;
	public  $consum_amount;
	public  $current_points ;
	public  $birthday;
	public  $province_code;
	public  $province_name;
	public  $city_code;
	public  $city_name;
	public  $district_code;
	public  $district_name;
	public  $adddress;
	public  $sex;

	//会员查询
	const API_CODE_OK 						= 2;
	const API_COUNT							= 1;


	const API_SUCCESS 						= 200;
	const API_ERROR 							= 1001;
	const API_FITER 							= 1006;
	const API_BE_OK 							= 1008;
	const API_NO_MEMBER 						= 1009;


	//会员查询
	const API_MEMBER_EMPTY					= 1005;
	const API_MEMBER_TIES					= 1012;

	//微信查询
	const API_WEIXIN_EMPTY					= 1007;
	//注册
	const API_REGISTER_EMPTY 					= 1002;
	const API_REGISTER_ERROR 					= 1003;
	const API_CREATE_MEMBER_ERROR 			= 1004;

	//修改
	const API_MODIFY_EMPTY					= 1010;
	const API_MODIFY_OK 						= 0;
	const API_MODIFY_ERROR 					= 1011;

	public static $defaultMsg	= [
		'200'	=>'成功',
		'1001'	=>'异常',
		'1006'	=>'参数错误',
		'1008'	=>'已注册',
		'1009'	=>'会员不存在',
	];

	public static $getMembers	= [
		'1005'	=>'查询会员结果为空',
		'1012'	=>'查询会员结果为多条',
	];

	public static $getMembersByWeiXinId	= [
		'1007'	=>'查询会员结果为空',
	];

	public static $register	= [
		'1002'	=>'注册结果返回空',
		'1004'	=>'注册失败',
	];

	public static $modify	= [
		'0'=>'修改成功',
		'1010'=>'修改结果为空',
		'1011'=>'修改错误'
	];


    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['mobile', 'pwd', 'wx_openid', 'wx_unionid', 'wx_nickname', 'wx_sex', 'x_province', 'wx_city', 'wx_country', 'wx_headimgurl', 'wx_privilege', 'memo', 'use_flg', 'email', 'creator', 'create_time', 'editor', 'edit_time', 'username', 'member_no','sex', 'member_source', 'consum_amount', 'current_points ', 'birthday', 'province_code', 'province_name', 'city_code', 'city_name', 'district_code', 'district_name', 'adddress' ], 'safe'],
			['mobile', 'trim'],
			//['mobile', 'trim'],正则手机号 TODO
        ];
    }

	public function retData($code,$function='defaultMsg',$data=[]){
		$fun = self::$$function;
		return [
			'code'=>$code,
			'msg'=>$fun[$code],
			'data'=>$data
		];
	}

	/**
	 * @param 获取会员信息
	 * @return array
	 */
	public function getMembers(){
		$params	= [
			'memberNo'=>$this->member_no? $this->member_no : '',
			'mobileNo'=>$this->mobile? $this->mobile : ''
		];

		try{
			//接口异常
			if(!$model	= SoapClient::soapClient('getMembers',$params)){
				throw new \Exception('接口无效');
			}

			//会员数据
			$getMembersResult =json_decode($model->GetMembersResult);
			return $this->retData(self::API_SUCCESS,'defaultMsg',$getMembersResult);

		}catch (\Exception $e){
			return $this->retData(self::API_ERROR,'defaultMsg',$e->getMessage());
		}
	}

	/**
	 * @param 根据微信ID获取会员信息
	 * @return  array
	 */
	public function getMembersByWeiXinId(){
		$params =[];
		if($this->wx_openid){
			$params	= [
				'weixinId'=>$this->wx_openid,
			];
		}

		//微信ID不能为空
		if(empty($params)){
			return $this->retData(self::API_FITER,'defaultMsg');
		}

		try{
			if(!$model	= SoapClient::soapClient('getMembersByWeiXinId',$params)){
				throw new \Exception('接口无效');
			}

			$getMembersResult =$model->GetMembersByWeiXinIdResult;
			if(!$getMembersResult){
				return $this->retData(self::API_WEIXIN_EMPTY,'getMembersByWeiXinId');
			}

			return $this->retData(self::API_SUCCESS,'defaultMsg',json_decode($getMembersResult));

		}catch ( \Exception $e){
			return $this->retData(self::API_ERROR,'defaultMsg',$e->getMessage());
		}
	}

	/*
	 * @param 注册线下用户
	 * @return array
	 */
	public  function register(){

		$params	=[
			'name'		=>$this->username 		? $this->username 		: '',
			'sex'		=>$this->sex		? $this->sex 		: '1',//默认给男士
			'mobile'	=>$this->mobile		? $this->mobile 	: '',
			//'birthday'	=>$this->birthday	? $this->birthday 	: '',
			'province'	=>$this->province_name	? $this->province_name 	: '',
			'city'		=>$this->city_name		? $this->city_name 		: '',
			'district'	=>$this->district_name	? $this->district_name 	: '',
			'adddress'	=>$this->adddress	? $this->adddress 	: '',
			'email'		=>$this->email		? $this->email 		: '',
			'weixinId'	=>$this->wx_openid	? $this->wx_openid 	: ''

		];

		try{

			if(!$register	= SoapClient::soapClient('register',$params))
				throw new \Exception('接口无效');

			if(!$registerResult =json_decode($register->RegisterResult))
				return $this->retData(self::API_REGISTER_EMPTY,'register');

			if(self::API_CODE_OK!=$registerResult->No)
				return $this->retData(self::API_REGISTER_ERROR,'register');

			return $this->retData(self::API_SUCCESS,'defaultMsg',$registerResult);

		}catch ( \Exception $e){

			return $this->retData(
				self::API_ERROR,
				'defaultMsg',
				$e->getMessage()
			);
		}
	}


    /**
     * @param 修改线下用户 微信id修改  必须传入手机号
	 * @param reutrn arrray
     */
    public function modify(){
		try{

			//修改的字段
			$paramsModel	=[
				'weixinId'	=>$this->wx_openid	? $this->wx_openid	: '',
				'name'		=>$this->username 	? $this->username 	: '',
				'sex'		=>$this->sex		? $this->sex 		: '1',//默认给男士
				'birthday'	=>$this->birthday	? $this->birthday	: '',
				'mobile'	=>$this->mobile		? $this->mobile		: '',
				'province'	=>$this->province_name	? $this->province_name 	: '',
				'city'		=>$this->city_name		? $this->city_name 		: '',
				'district'	=>$this->district_name	? $this->district_name 	: '',
				'adddress'	=>$this->adddress	? $this->adddress 	: '',
				'email'		=>$this->email		? $this->email 		: '',
			];

			return true;

			/*
			if(!$modifyInfo	=SoapClient::soapClient('modify',$paramsModel)){
				throw new \Exception('接口异常');
			}

			if(!$modifyResult =json_decode($modifyInfo->ModifyResult)){
				return $this->retData(self::API_MODIFY_EMPTY,'modify');
			}
			if(self::API_MODIFY_OK!=$modifyResult->No){
				return $this->retData(self::API_MODIFY_ERROR,'modify');
			}
			redisHelper::delMasterRelateGenerMerber($modifyResult->Member_no);	//TODO 传入对应的会员
			redisHelper::delMastserMobileInfo($this->mobile);					//对应的手机号
			return $this->retData(self::API_SUCCESS,'defaultMsg',$modifyResult);*/

		}catch ( \Exception $e){

			return $this->retData(self::API_ERROR,'defaultMsg',$e->getMessage());
		}
    }
}